package com.zjsru.plan2024.oneday;

import java.util.Arrays;

/**
 * 661. 图片平滑器
 *
 * @Author: cookLee
 * @Date: 2024-11-18
 */
public class ImageSmoother {

    /**
     * 主
     * \
     * 输入:img = [[1,1,1],[1,0,1],[1,1,1]]
     * 输出:[[0, 0, 0],[0, 0, 0], [0, 0, 0]]
     * 解释:
     * 对于点 (0,0), (0,2), (2,0), (2,2): 平均(3/4) = 平均(0.75) = 0
     * 对于点 (0,1), (1,0), (1,2), (2,1): 平均(5/6) = 平均(0.83333333) = 0
     * 对于点 (1,1): 平均(8/9) = 平均(0.88888889) = 0
     * \
     * 输入: img = [[100,200,100],[200,50,200],[100,200,100]]
     * 输出: [[137,141,137],[141,138,141],[137,141,137]]
     * 解释:
     * 对于点 (0,0), (0,2), (2,0), (2,2): floor((100+200+200+50)/4) = floor(137.5) = 137
     * 对于点 (0,1), (1,0), (1,2), (2,1): floor((200+200+50+200+100+100)/6) = floor(141.666667) = 141
     * 对于点 (1,1): floor((50+200+200+200+200+100+100+100+100)/9) = floor(138.888889) = 138
     * \
     *
     * @param args args
     */
    public static void main(String[] args) {
        ImageSmoother imageSmoother = new ImageSmoother();
        int[][] img = new int[][]{{1, 1, 1}, {1, 0, 1}, {1, 1, 1}};
        System.out.println(Arrays.deepToString(imageSmoother.imageSmoother(img)));
    }

    private static int[][] DIRS = new int[][]{{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 0}, {0, 1}, {1, -1}, {1, 0}, {1, 1}};

    public int[][] imageSmoother(int[][] img) {
        int len = img.length;
        int width = img[0].length;
        int[][] ans = new int[len][width];
        for (int i = 0; i < len; i++) {
            for (int j = 0; j < width; j++) {
                ans[i][j] = this.healper(img, i, j);
            }
        }
        return ans;
    }

    private int healper(int[][] img, int i, int j) {
        int sum = 0;
        int count = 0;
        //模拟不同位置
        for (int[] dir : DIRS) {
            int x = i + dir[0];
            int y = j + dir[1];
            if (x >= 0 && x < img.length && y >= 0 && y < img[0].length) {
                sum += img[x][y];
                count++;
            }
        }
        return sum / count;
    }

}
